package ru.megazlo.basemvc.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import ru.megazlo.basemvc.controllers.base.Action;
import ru.megazlo.basemvc.controllers.base.BaseController;
import ru.megazlo.basemvc.viewmodel.RegisterUser;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Controller
public class AccountController extends BaseController {

	@Autowired
	private UserDetailsService userDetailsService;

	@RequestMapping("/account/loginForm")
	@Action(elem = DIALOG)
	public Object loginForm(HttpServletResponse response) {
		return getResult(response);
	}

	@RequestMapping("/account/register")
	@Action(elem = DIALOG)
	public Object register(@Valid RegisterUser registerUser, BindingResult binding, HttpServletResponse response) {
		if (isPost()) {
			if (binding.hasErrors()) {
				/*ModelAndView mv = new ModelAndView();
				mv.addObject("registerUser", registerUser);
				mv.addObject(BindingResult.class.getName() + ".registerUser", binding);*/
				return getResult("registerUser", registerUser, response);
			}
			return redirect("/", response);
		}
		return getResult("registerUser", registerUser, response);
	}

	@RequestMapping("/account/cropPicture")
	@Action(elem = DIALOG)
	public Object cropPicture(HttpServletResponse response) {
		return getResult(response);
	}

	@RequestMapping("/account/viewProfile")
	@Action
	public Object viewProfile(HttpServletResponse response) {
		return getResult(response);
	}

	@RequestMapping("/account/logOut")
	public Object logOut() {
		return "redirect:/";
	}

	@RequestMapping("/account/logIn/{logget}")
	@Action(elem = ALERT, view = "account/loginFalure")
	public Object logIn(HttpServletResponse response, @PathVariable("logget") Boolean logget) {
		return logget ? redirect("/", response) : getResult(response);
	}

	public void setUserDetailsService(UserDetailsService userDetailsService) {
		this.userDetailsService = userDetailsService;
	}
}
